package com.lognsoft.shell;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public class StringLineOutputStream extends OutputStream 
{
	private StringBuffer _buffer = new StringBuffer();
	private ArrayList<String> _lines = null;
	
public StringLineOutputStream()
{
}
	
public StringLineOutputStream(ArrayList<String> lines)
{
	_lines = lines;
}

public void onRecvLine(String line)
{
	if (_lines == null)
	{
		_lines = new ArrayList<String>();
	}
	
	_lines.add(line);
}

public ArrayList<String> getLines()
{
	return _lines;
}

public void write(int b) throws IOException
{
	if (b == 0 || b == '\r')
	{
		return;
	}
	    	
	if (b == '\n')
	{
		onRecvLine(_buffer.toString());
		_buffer.delete(0, _buffer.length());
	}
	else
	{
		_buffer.append( (char) b );
	}
}
    
public void close() throws IOException 
{
	if (_buffer.length() > 0 )
	{
		write('\n');
	}
}

};
